package com.sleepcamel.resteasyConfigurator.model;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ListChooserModel<O> {

//	private List<O> allChoices;
	private List<O> selectedChoices;
	private List<O> unSelectedChoices;

	public void setSelectedChoices(List<O> selectedChoices){
		this.selectedChoices = selectedChoices;
		calculateUnselectedChoices();
	}
	
	public void setUnSelectedChoices(List<O> unSelectedChoices){
		this.unSelectedChoices = unSelectedChoices;
		calculateUnselectedChoices();
	}

	private void calculateUnselectedChoices() {
		if ( selectedChoices != null && unSelectedChoices != null ){
			for(O choice:selectedChoices)
				unSelectedChoices.remove(choice);
		}
	}

	public List<O> getSelectedChoices() {
		return selectedChoices;
	}

	public List<O> getUnSelectedChoices() {
		return unSelectedChoices;
	}
	
	public void addSelectedChoice(O choice){
		selectedChoices.add(choice);
		unSelectedChoices.remove(choice);
	}
	
	public void removeSelectedChoice(O choice){
		selectedChoices.remove(choice);
		unSelectedChoices.add(choice);
	}
	
	public void addSelectedChoices(O [] choices){
		addSelectedChoices(Arrays.asList(choices));
	}
	
	public void addSelectedChoices(List<O> choices){
		for(O choice:choices){
			addSelectedChoice(choice);
		}
	}
	
	public void removeSelectedChoices(O [] choices){
		removeSelectedChoices(Arrays.asList(choices));
	}
	
	public void removeSelectedChoices(List<O> choices){
		for(O choice:choices){
			removeSelectedChoice(choice);
		}
	}

	public void removeAllSelectedChoices() {
		Iterator<O> iterator = selectedChoices.iterator();
		while(iterator.hasNext()){
			O next = iterator.next();
			iterator.remove();
			unSelectedChoices.add(next);
		}
	}
}
